﻿using HuiZe.education.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace HuiZe.education.Resource.Controls
{
    [TemplatePart(Name = PART_Subject, Type = typeof(CheckList))]
    [TemplatePart(Name = PART_Class, Type = typeof(CheckList))]
    [TemplatePart(Name = tvProperties, Type = typeof(TreeView))]
    public class MultiComboBox : ComboBox
    {
        private const string PART_Subject = "PART_Subject";
        private const string PART_Class = "PART_Class";
        private const string ItemHostStackPanel = "ItemHostStackPanel";
        private const string tvProperties = "tvProperties";
        public string gradeIds { get; set; }
        public string subjectIds { get; set; }
        public string ClassIds { get; set; }
        public ObservableCollection<ClassModel3> Grades { get; set; } //班级

        private CheckList _listboxSubject;
        private CheckList _listBoxClass;
        private StackPanel _stackPanel;
        //private TreeView _treeView;

        public List<CategoryModel3> selectedCheckList = new List<CategoryModel3>();

        public List<ClassModel3> ClassModellist = new List<ClassModel3>();
        public int columnWidth { get; set; } = 0;
        public int columnwidth1 { get; set; } = 300;
        bool isload = false;

        public ObservableCollection<TreeData> treeDatas
        {
            get { return (ObservableCollection<TreeData>)GetValue(treeDatasProperty); }
            set { SetValue(treeDatasProperty, value); }
        }
        public static readonly DependencyProperty treeDatasProperty =
            DependencyProperty.Register("treeDatas", typeof(ObservableCollection<TreeData>), typeof(MultiComboBox), new PropertyMetadata(null));

        public MultiComboBox()
        {
            Loaded += MultiComboBox_Loaded;
            Unloaded += MultiComboBox_Unloaded;

        }

        private void MultiComboBox_Unloaded(object sender, RoutedEventArgs e)
        {
            _listboxSubject = null;
            _stackPanel = null;
        }

        private void MultiComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (!isload)
            {
                _listboxSubject = GetTemplateChild(PART_Subject) as CheckList;
                if (_listboxSubject != null)
                    _listboxSubject.SelectedItemChanged += _listbox_SelectedItemChanged;
                _listBoxClass = GetTemplateChild(PART_Class) as CheckList;
                if (_listBoxClass != null)
                    _listBoxClass.SelectedItemChanged += _listbox_SelectedItemChanged;
                isload = true;


            }
            if (_stackPanel == null)
                _stackPanel = GetTemplateChild(ItemHostStackPanel) as StackPanel;
            //LoadData();
        }
        private void LoadParam()
        {

            if (_listboxSubject == null && _listBoxClass == null)
            {
                _listboxSubject = GetTemplateChild(PART_Subject) as CheckList;
                //if (_listboxSubject != null)
                //    _listboxSubject.SelectedItemChanged += _listbox_SelectedItemChanged;
            }
            if (_listBoxClass == null && _listboxSubject == null)
            {
                _listBoxClass = GetTemplateChild(PART_Class) as CheckList;
                //if (_listBoxClass != null)
                //    _listBoxClass.SelectedItemChanged += _listbox_SelectedItemChanged;
            }
            if (_stackPanel == null)
                _stackPanel = GetTemplateChild(ItemHostStackPanel) as StackPanel;

        }

        public void LoadData()
        {

            if (_listboxSubject != null&& subjectIds!=null)
            {
                string[] subjectList = subjectIds.Split(',');
                foreach (var item in _listboxSubject.Items)
                {
                    //    CheckListItem checkListItem = item as CheckListItem;
                    //    CategoryModel2 categoryModel = checkListItem.DataContext as CategoryModel2;

                    CheckListItem checkListItem = new CheckListItem(); ;
                    CategoryModel3 categoryModel = item as CategoryModel3;
                    checkListItem.DataContext = categoryModel;
                    checkListItem.IsChecked = true;
                    Button button = new Button()
                    {
                        Style = (Style)FindResource("textButton"),
                        Height = 28,
                        Margin = new Thickness(10, 0, 0, 0),
                        Content = categoryModel.name,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Tag = checkListItem,
                    };
                    //_listboxSubject.checkListItems.Add(checkListItem);
                    button.Click += Button_Click;
                    _stackPanel.Children.Add(button);
                    //selectedCheckList.Add(checkListItem.DataContext as CategoryModel2);

                }
            }
            if (_listBoxClass != null&& gradeIds!=null)
            {
                string[] gradeList = gradeIds.Split(',');

                ClassIds = ClassIds.Substring(1, ClassIds.Length - 1);
                ClassIds = ClassIds.Substring(0, ClassIds.Length - 1);
                string[] ClassList = ClassIds.Split(']');

                for (int j = 0; j < ClassList.Length; j++)
                {
                    string a = ClassList[j];

                    a.Replace("[", "");
                    a.Replace(",[", "");
                    ClassList[j] = a;

                }
                foreach (var item in _listBoxClass.Items)
                {
                    CheckListItem checkListItem = new CheckListItem();
                    ClassModel3 classModel = item as ClassModel3;
                    checkListItem.DataContext = classModel;
                    Button button = new Button()
                    {
                        Style = (Style)FindResource("textButton"),
                        Height = 28,
                        Margin = new Thickness(10, 0, 0, 0),
                        Content = classModel.DisPlayName,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Tag = checkListItem,
                    };
                    button.Click += Button_Click;
                    _stackPanel.Children.Add(button);

                    //ClassModellist.Add(checkListItem.DataContext as ClassModel2);
                }



            }





        }

        private void _listbox_SelectedItemChanged(object sender, RoutedEventArgs e)
        {
            LoadParam();
            CheckListItem checkListItem = CheckList.curCheckListItem;
            if (checkListItem.IsChecked)
            {
                if (_stackPanel != null)
                {
                    object obj = checkListItem.DataContext;
                    Type type = obj.GetType();
                    object propertyValue = null;
                    if (type != null)
                    {
                        if (_listboxSubject != null)
                        {
                            var propertyInfo = type.GetProperty("name");
                            if (propertyInfo != null)
                                propertyValue = propertyInfo.GetValue(obj);
                        }
                        else
                        {
                            var propertyInfo = type.GetProperty("name");
                            if (propertyInfo != null)
                                propertyValue = propertyInfo.GetValue(obj);
                        }
                    }
                    Button button = new Button()
                    {
                        Style = (Style)FindResource("textButton"),
                        Height = 28,
                        FontSize = 12,
                        Margin = new Thickness(10, 0, 0, 0),
                        Content = propertyValue,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Tag = checkListItem,
                    };
                    Button button2 = new Button()
                    {
                        Style = (Style)FindResource("textButton2"),
                        Width = 60,
                        FontSize = 12,
                        Height = 28,
                        Name = "SL",
                        Margin = new Thickness(5, 0, 0, 0),
                        Content = "...",
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Tag = checkListItem,
                    };
                    button.Click += Button_Click;
                    if (_listboxSubject != null)
                    {
                        if (_stackPanel.Children.Count == 3)
                        {
                            button.Visibility = Visibility.Collapsed;
                            _stackPanel.Children.Add(button);
                            _stackPanel.Children.Add(button2);
                        }
                        else if (_stackPanel.Children.Count >= 4)
                        {
                            button.Visibility = Visibility.Collapsed;
                            _stackPanel.Children.Add(button);
                        }
                        else
                        {
                            _stackPanel.Children.Add(button);

                        }
                    }
                    else
                    {

                        if (_stackPanel.Children.Count == 2)
                        {
                            button.Visibility = Visibility.Collapsed;
                            _stackPanel.Children.Add(button);
                            _stackPanel.Children.Add(button2);
                        }
                        else if (_stackPanel.Children.Count >= 3)
                        {
                            button.Visibility = Visibility.Collapsed;
                            _stackPanel.Children.Add(button);
                        }
                        else
                        {
                            _stackPanel.Children.Add(button);

                        }

                    }
                    if (_listboxSubject != null)
                    {
                        selectedCheckList.Add(checkListItem.DataContext as CategoryModel3);
                    }
                    else
                    {
                        ClassModellist.Add(checkListItem.DataContext as ClassModel3);

                    }
                    //var classModel = checkListItem.DataContext as HuiZe.education.Model.ClassModel;
                    //ClassModel2 classModel2 = new ClassModel2
                    //{
                    //    id= classModel

                    //};

                }
            }
            else
            {
                foreach (var item in _stackPanel.Children)
                {
                    CheckListItem _checkListItem = (item as Button).Tag as CheckListItem;
                    //CheckListItem _checkListItem2 = new CheckListItem();
                    if (_checkListItem != null && _checkListItem == checkListItem)
                    {
                        //_checkListItem.IsChecked = false;
                        //_stackPanel.Children.Remove(item as Button);
                        if (_listboxSubject != null)
                        {

                            //_checkListItem.IsChecked = false;
                            selectedCheckList.Remove(_checkListItem.DataContext as CategoryModel3);
                            _stackPanel.Children.Remove(item as Button);
                            if (_stackPanel.Children.Count >= 2)
                            {
                                if (_stackPanel.Children.Count == 4 || _stackPanel.Children.Count == 3)
                                {
                                    if (_stackPanel.Children.Count == 4)
                                    {
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Name == "SL")
                                            {
                                                _stackPanel.Children.Remove(button);
                                                break;
                                            }
                                        }
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Visibility == Visibility.Collapsed)
                                            {
                                                button.Visibility = Visibility.Visible;
                                                break;
                                            }
                                        }

                                    }
                                    else
                                    {
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Name == "SL")
                                            {
                                                _stackPanel.Children.Remove(button);
                                                break;
                                            }
                                        }
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Visibility == Visibility.Collapsed)
                                            {
                                                button.Visibility = Visibility.Visible;
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //移除省略按钮
                                    Button SLBtn = null;
                                    foreach (var item0 in _stackPanel.Children)
                                    {
                                        Button button = item0 as Button;
                                        if (button.Name == "SL")
                                            SLBtn = button;
                                    }
                                    if (SLBtn != null)
                                        _stackPanel.Children.Remove(SLBtn);

                                    foreach (var item0 in _stackPanel.Children)
                                    {
                                        Button button = item0 as Button;
                                        if (button.Visibility == Visibility.Collapsed)
                                        {
                                            button.Visibility = Visibility.Visible;
                                            break;
                                        }
                                    }

                                    if (_stackPanel.Children.Count >= 4)
                                    {
                                        _stackPanel.Children.Add(SLBtn);
                                    }
                                }
                            }

                            //if (selectedCheckList.Count > 2)
                            //{
                            //    //移除省略按钮
                            //    Button SLBtn = null;
                            //    foreach (var item0 in _stackPanel.Children)
                            //    {
                            //        Button button = item0 as Button;
                            //        if (button.Name == "SL")
                            //            SLBtn = button;
                            //    }
                            //    if (SLBtn != null)
                            //        _stackPanel.Children.Remove(SLBtn);

                            //    foreach (var category in selectedCheckList)
                            //    {
                            //        //添加button
                            //        bool isAdd = true;
                            //        foreach (var item0 in _stackPanel.Children)
                            //        {
                            //            Button button = item0 as Button;
                            //            if (category.name == button.Content)
                            //            {
                            //                isAdd = false;
                            //                break;
                            //            }
                            //        }
                            //        if (isAdd&& _stackPanel.Children.Count<=1)
                            //        {
                            //            _checkListItem2.DataContext = category;
                            //            _checkListItem2.IsChecked = true;
                            //             Button button = new Button()
                            //            {
                            //                Style = (Style)FindResource("textButton"),
                            //                Height = 28,
                            //                FontSize = 12,
                            //                Margin = new Thickness(10, 0, 0, 0),
                            //                Content = category.name,
                            //                HorizontalAlignment = HorizontalAlignment.Left,
                            //                Tag = _checkListItem2,
                            //            };
                            //            _stackPanel.Children.Add(button);
                            //            button.Click += Button_Click;
                            //        }

                            //    }

                            //    if (selectedCheckList.Count >= 3)
                            //    {
                            //        _stackPanel.Children.Add(SLBtn);
                            //    }

                            //}
                            //else if (selectedCheckList.Count == 2)
                            //{
                            //    Button SLBtn = null;
                            //    foreach (var item0 in _stackPanel.Children)
                            //    {
                            //        Button button = item0 as Button;
                            //        if (button.Name == "SL")
                            //            SLBtn = button;
                            //    }
                            //    if (SLBtn != null)
                            //        _stackPanel.Children.Remove(SLBtn);


                            //    foreach (var category in selectedCheckList)
                            //    {

                            //        //添加button
                            //        bool isAdd = true;
                            //        foreach (var item0 in _stackPanel.Children)
                            //        {
                            //            Button button = item0 as Button;
                            //            if (category.name == button.Content)
                            //            {
                            //                isAdd = false;
                            //                break;
                            //            }
                            //        }
                            //        if (isAdd && _stackPanel.Children.Count <= 1)
                            //        {
                            //            _checkListItem2.DataContext = category;
                            //            _checkListItem2.IsChecked = true;
                            //            Button button = new Button()
                            //            {
                            //                Style = (Style)FindResource("textButton"),
                            //                Height = 28,
                            //                FontSize = 12,
                            //                Margin = new Thickness(10, 0, 0, 0),
                            //                Content = category.name,
                            //                HorizontalAlignment = HorizontalAlignment.Left,
                            //              Tag = _checkListItem,
                            //            };
                            //            _stackPanel.Children.Add(button);
                            //            button.Click += Button_Click;
                            //        }

                            //    }


                            //}

                        }
                        if (_listBoxClass != null)
                        {
                            ClassModellist.Remove(_checkListItem.DataContext as ClassModel3);
                            _stackPanel.Children.Remove(item as Button);
                            if (_stackPanel.Children.Count >= 2)
                            {
                                if (_stackPanel.Children.Count == 3 || _stackPanel.Children.Count == 2)
                                {
                                    if (_stackPanel.Children.Count == 3)
                                    {
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Name == "SL")
                                            {
                                                _stackPanel.Children.Remove(button);
                                                break;
                                            }
                                        }
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Visibility == Visibility.Collapsed)
                                            {
                                                button.Visibility = Visibility.Visible;
                                                break;
                                            }
                                        }

                                    }
                                    else
                                    {
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Name == "SL")
                                            {
                                                _stackPanel.Children.Remove(button);
                                                break;
                                            }
                                        }
                                        foreach (var item0 in _stackPanel.Children)
                                        {
                                            Button button = item0 as Button;
                                            if (button.Visibility == Visibility.Collapsed)
                                            {
                                                button.Visibility = Visibility.Visible;
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //移除省略按钮
                                    Button SLBtn = null;
                                    foreach (var item0 in _stackPanel.Children)
                                    {
                                        Button button = item0 as Button;
                                        if (button.Name == "SL")
                                            SLBtn = button;
                                    }
                                    if (SLBtn != null)
                                        _stackPanel.Children.Remove(SLBtn);

                                    foreach (var item0 in _stackPanel.Children)
                                    {
                                        Button button = item0 as Button;
                                        if (button.Visibility == Visibility.Collapsed)
                                        {
                                            button.Visibility = Visibility.Visible;
                                            break;
                                        }
                                    }

                                    if (_stackPanel.Children.Count >= 3)
                                    {
                                        _stackPanel.Children.Add(SLBtn);
                                    }
                                }
                            }


                            //if (ClassModellist.Count > 2)
                            //{
                            //    //移除省略按钮
                            //    Button SLBtn = null;
                            //    foreach (var item0 in _stackPanel.Children)
                            //    {
                            //        Button button = item0 as Button;
                            //        if (button.Name == "SL")
                            //            SLBtn = button;
                            //    }
                            //    if (SLBtn != null)
                            //        _stackPanel.Children.Remove(SLBtn);
                            //    foreach (var classModel in ClassModellist)
                            //    {


                            //        //添加button
                            //        bool isAdd = true;
                            //        foreach (var item0 in _stackPanel.Children)
                            //        {
                            //            Button button = item0 as Button;
                            //            if (classModel.DisPlayName == button.Content)
                            //            {
                            //                isAdd = false;
                            //                break;
                            //            }
                            //        }
                            //        if (isAdd &&  _stackPanel.Children.Count <= 1)
                            //        {

                            //            _checkListItem2.DataContext = classModel;
                            //            _checkListItem2.IsChecked = true;
                            //            Button button = new Button()
                            //            {
                            //                Style = (Style)FindResource("textButton"),
                            //                Height = 28,
                            //                FontSize = 12,
                            //                Margin = new Thickness(10, 0, 0, 0),
                            //                Content = classModel.DisPlayName,
                            //                HorizontalAlignment = HorizontalAlignment.Left,
                            //                Tag = _checkListItem,
                            //            };
                            //            _stackPanel.Children.Add(button);
                            //            button.Click += Button_Click;
                            //        }


                            //    }
                            //    if (ClassModellist.Count >=3)
                            //    {
                            //        _stackPanel.Children.Add(SLBtn);
                            //    }

                            //}
                            //else if (ClassModellist.Count == 2)
                            //{
                            //    Button SLBtn = null;
                            //    foreach (var item0 in _stackPanel.Children)
                            //    {
                            //        Button button = item0 as Button;
                            //        if (button.Name == "SL")
                            //            SLBtn = button;
                            //    }
                            //    if (SLBtn != null)
                            //        _stackPanel.Children.Remove(SLBtn);

                            //    foreach (var classModel in ClassModellist)
                            //    {


                            //        //添加button
                            //        bool isAdd = true;
                            //        foreach (var item0 in _stackPanel.Children)
                            //        {
                            //            Button button = item0 as Button;
                            //            if (classModel.DisPlayName == button.Content)
                            //            {
                            //                isAdd = false;
                            //                break;
                            //            }
                            //        }
                            //        if (isAdd && _stackPanel.Children.Count <= 1)
                            //        {
                            //            _checkListItem2.DataContext = classModel;
                            //            _checkListItem2.IsChecked = true;
                            //            Button button = new Button()
                            //            {
                            //                Style = (Style)FindResource("textButton"),
                            //                Height = 28,
                            //                FontSize = 12,
                            //                Margin = new Thickness(10, 0, 0, 0),
                            //                Content = classModel.DisPlayName,
                            //                HorizontalAlignment = HorizontalAlignment.Left,
                            //                Tag = _checkListItem,
                            //            };
                            //            _stackPanel.Children.Add(button);
                            //            button.Click += Button_Click;
                            //        }


                            //    }

                            //}

                        }

                        break;
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckListItem _checkListItem = (sender as Button).Tag as CheckListItem;
            //_checkListItem.IsChecked = true;
            _checkListItem.IsChecked = false;

        }
    }
    public class TreeData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public int id { get; set; }
        public string DisplayName { get; set; }
        public TreeData Parent { get; set; }
        public List<TreeData> Children { get; set; }
        //最终可选节点
        public List<TreeData> LastChildren { get; set; }

        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                //PropertyChanged.Invoke(this, new PropertyChangedEventHandler(Checked(this,new PropertyChangedEventArgs(""))));
            }
        }

        public void Checked(object obj, PropertyChangedEventArgs e)
        {

        }
        public TreeData()
        {
            Children = new List<TreeData>();
        }

    }
}
